#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h> 
#include <time.h>
#include "sqlite3.h"

#include "server.h"
#include "net.h"
//定义全局变量
MSG msg;
struct sqlite3 * pdb = NULL;
char *pErrMsg = NULL;
char** ppTable = NULL;
int iRow = 0;
int iColumn = 0;
int iResult;

//登录
int login(char * p)
{    

    printf("p===%s\n",p);
    //数据库判断
    if (NULL == pdb)
	{
		printf("数据库没有打开 ，返回失败error\r\n");
		return ERROR;
	}

    if (NULL == p)
    {
        printf("p error");
        return ERROR;
    }

    //检测账号是否存在

    char sqlSearch[DATASIZE] = {0};
    
    printf("用户输入的密码是：%s\n",p);
    sprintf(sqlSearch,"SELECT * FROM pass WHERE passWord = \'%s\';",p);
    printf("%s\n",sqlSearch);
    iResult = sqlite3_get_table(pdb, sqlSearch, &ppTable, &iRow, &iColumn,&pErrMsg);
    if(SQLITE_OK != iResult)
    {
        printf("%s\n", pErrMsg);
        //sqlite3_free(pErrMsg);
    }
    sqlite3_free(pErrMsg);
    printf("select sql row %d column %d\n", iRow, iColumn); 
    if (NULL != ppTable)
    {
        int i = 1;
        for (; i <= iRow; i++)
        {
            int j = 0;
            for (; j < iColumn; j++)
            {
                if (NULL != ppTable[i * iColumn + j])
                {
                    printf("%s    ", ppTable[i * iColumn + j]);  
                }  
            }
            printf("\n");
        }
    } 
    if (iRow > 0 && iColumn > 0)
	{	
        printf("用户登录成功\n");
        sqlite3_free_table(ppTable);
        return OK;
    }
    else
    {
        sqlite3_free_table(ppTable);
        return ERROR;
    }
}

//初始化数据库
int initDatabase()
{
	char *pPath = NULL;
    char dbName[] = "/mysqlite.db";
	
	API();
	printf("打开数据库，创建表，准备表中的数据\r\n");
	//准备数据库的名字
	/*db name start*/    
    pPath = (char *) malloc (FILEPATH_MAX + strlen(dbName));
    if (NULL == pPath)
    {
        return -1;
    }
    memset(pPath, 0, FILEPATH_MAX + strlen(dbName));
    if (NULL == getcwd(pPath, FILEPATH_MAX))
	{
		return -1;
	}
    printf("%s %d\n",pPath,strlen(pPath));
    memcpy(pPath + strlen(pPath), dbName, strlen(dbName));
    printf("%s %d\n",pPath,strlen(pPath));
    /*db name end*/
	
	//打开数据库 	
    int iResult = sqlite3_open((const char *)pPath,&pdb);
    if(SQLITE_OK != iResult || NULL == pdb)
    {
      printf("invoke sqlite3_open function error!\n");
      free(pPath);
      pPath = NULL;
      return -1;
    }
	
	//---------------以下仅是测试-------------------
	//select table
    char sqlSelect[] = "select * from pass";
    char** ppTable = NULL;
    int iRow = 0;
    int iColumn = 0;
    iResult = sqlite3_get_table(pdb, sqlSelect, &ppTable, &iRow, &iColumn,&pErrMsg);
	
    if (SQLITE_OK != iResult)
    {
        printf("%s\n", pErrMsg);
    }
    sqlite3_free(pErrMsg);
    printf("select sql row %d column %d\n", iRow, iColumn);
    if (NULL != ppTable)
    {
        int i = 1;
        for (; i <= iRow; i++)
        {
            int j = 0;
            for (; j < iColumn; j++)
            {
                if (NULL != ppTable[i * iColumn + j])
                {
                    printf("%s    ",ppTable[i * iColumn + j]);
                }
                
            }
            printf("\n");
        }
    }
	 //close db
    //free memory
    free(pPath);
    pPath = NULL;
	//---------------以上仅是测试-------------------
	return 0;
}

//关闭数据库
void closeDatabase()
{
    sqlite3_close(pdb);
    pdb = NULL;
}

//灯光控制
int light(char * status,int wifi1)
{
    printf("status is -------->%s\r\n",status);


    if(NULL == status)
    {
        return ERROR;
    }

    if(0 == strcmp(status,"open"))
    {    
        memset(&msg, 0, sizeof(MSG));
        msg.op = 4;
        strcpy(msg.data, "this is server func!\r\n");
        send(wifi1, &msg, sizeof(msg), 0);
        printf("send to wifi1 ok!\r\n");
       
    } 
    return OK;
}